' FILESYSTEMIMAGELIST.VB
' File System Access for the Cold Storage component.
'
' Copyright (C) 2002 Microsoft Corporation 
' All rights reserved. 
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" 
' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
' INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
' MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
' 
' Date: 03/13/02
' Authors: Allen Wagner, Marcelo Uemura, David Willson

Option Explicit On 

Imports System
Imports System.Collections
Imports System.Windows.Forms

Friend Class StatusUpdate
    Inherits System.ComponentModel.Component

    Private Const CLASS_STATUSUPDATE As String = "StatusUpdate"
    Private theStatusBar As StatusBar

    Public Sub New( _
        ByVal container As System.ComponentModel.IContainer, _
        ByVal status As StatusBar)

        container.Add(Me, CLASS_STATUSUPDATE)
        theStatusBar = status

    End Sub


    Public Sub New( _
        ByVal container As System.ComponentModel.IContainer)

        container.Add(Me, CLASS_STATUSUPDATE)
        theStatusBar = Nothing

    End Sub

    Public Property [StatusBar]() As StatusBar
        Get
            Return theStatusBar
        End Get

        Set(ByVal Value As StatusBar)
            theStatusBar = Value
        End Set
    End Property


    Public Sub Update(ByVal updateText As String)

        If Not (StatusBar Is Nothing) Then
            theStatusBar.Text = updateText
        End If

    End Sub

End Class

Public Class FileSystemImageList
    Inherits System.ComponentModel.Component

    Private theShell32 As New Shell32

    Public Const FolderClosedIconIndex As Integer = 0
    Public Const FolderOpenIconIndex As Integer = 1
    Public Const FileGenericIconIndex As Integer = 2

    Private Const CLASS_FILESYSTEMIMAGELIST As String = _
        "FileSystemImageList"

    Private theExtensionIconIndex As Hashtable
    Private theSmallImageList As ImageList
    Private theLargeImageList As ImageList

    Public Sub New( _
        ByVal container As System.ComponentModel.IContainer)

        container.Add(Me, CLASS_FILESYSTEMIMAGELIST)
        InitializeComponent()
    End Sub
    Public Sub New()
        InitializeComponent()
    End Sub

    Sub InitializeComponent()

        theSmallImageList = New ImageList
        theSmallImageList.ColorDepth = _
            System.Windows.Forms.ColorDepth.Depth8Bit
        theSmallImageList.ImageSize = _
            New System.Drawing.Size(16, 16)
        theSmallImageList.TransparentColor = _
            System.Drawing.Color.Transparent

        theLargeImageList = New ImageList

        theLargeImageList.ColorDepth = _
            System.Windows.Forms.ColorDepth.Depth8Bit
        theLargeImageList.ImageSize = _
            New System.Drawing.Size(64, 64)
        theLargeImageList.TransparentColor = _
            System.Drawing.Color.Transparent

        theExtensionIconIndex = _
            New System.Collections.Hashtable

        AddFolderIcon(Convert.ToUInt32(0))

        AddFolderIcon(Convert.ToUInt32(Shell32.SHGFI_OPENICON))
        AddFileIcon(Nothing)

    End Sub

    Public ReadOnly Property SmallImageList() As ImageList
        Get
            Return theSmallImageList
        End Get
    End Property


    Public ReadOnly Property LargeImageList() As ImageList
        Get
            Return theLargeImageList
        End Get
    End Property

    Private Sub AddFolderIcon( _
        ByVal flags As System.UInt32)

        ' ToDo: Unsigned Integers such as System.UInt32
        ' are not CLS compliant types.
        flags = Convert.ToUInt32( _
            Convert.ToInt32(flags) Or _
            Convert.ToInt32(Shell32.SHGFI_ICON) Or _
            Convert.ToInt32(Shell32.SHGFI_USEFILEATTRIBUTES))

        ' Get the folder icon.
        Dim shfi As New Shell32.SHFILEINFO
        Shell32.SHGetFileInfo( _
            Nothing, _
            Shell32.FILE_ATTRIBUTE_DIRECTORY, _
            shfi, _
            Convert.ToUInt32( _
                System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            flags)

        Dim icon As System.Drawing.Icon = _
            System.Drawing.Icon.FromHandle(shfi.hIcon)
        theLargeImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

        ' Get the small folder icon.
        flags = Convert.ToUInt32(Convert.ToInt32(flags) Or _
            Convert.ToInt32(Shell32.SHGFI_SMALLICON))

        Shell32.SHGetFileInfo( _
            Nothing, _
            Shell32.FILE_ATTRIBUTE_DIRECTORY, _
            shfi, _
            Convert.ToUInt32( _
                System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            flags)

        icon = System.Drawing.Icon.FromHandle(shfi.hIcon)
        theSmallImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

    End Sub

    Private Function AddFileIcon(ByVal name As String) As Integer

        ' Get the large generic file icon.
        Dim shfi As New Shell32.SHFILEINFO
        Shell32.SHGetFileInfo( _
            name, _
            Shell32.FILE_ATTRIBUTE_NORMAL, _
            shfi, _
            System.Convert.ToUInt32( _
                Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            Convert.ToUInt32( _
                Convert.ToInt32( _
                    Shell32.SHGFI_USEFILEATTRIBUTES) Or _
                Convert.ToInt32(Shell32.SHGFI_ICON)))

        Dim icon As System.Drawing.Icon = _
            System.Drawing.Icon.FromHandle(shfi.hIcon)
        theLargeImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

        ' Get the small generic file icon.
        shfi = New Shell32.SHFILEINFO
        Shell32.SHGetFileInfo( _
            name, _
            Shell32.FILE_ATTRIBUTE_NORMAL, _
            shfi, _
            Convert.ToUInt32( _
                System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            Convert.ToUInt32( _
                Convert.ToInt32(Shell32.SHGFI_ICON) Or _
                Convert.ToInt32(Shell32.SHGFI_SMALLICON) Or _
                Convert.ToInt32(Shell32.SHGFI_USEFILEATTRIBUTES)))

        icon = System.Drawing.Icon.FromHandle(shfi.hIcon)
        theSmallImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

        If Not (name Is Nothing) Then
            Dim iconindex As Integer = _
            SmallImageList.Images.Count - 1

            theExtensionIconIndex.Add(name, iconindex)
            Return iconindex

        End If

        Return FileGenericIconIndex

    End Function

    Public Function GetTheFileIconIndex( _
        ByVal filename As String) As Integer
        Dim index As Integer = filename.LastIndexOf(".")

        If index > -1 Then
            Dim extension As String = filename.Substring(index)

            If Not theExtensionIconIndex.ContainsKey(extension) Then
                Return AddFileIcon(extension)
            Else
                Return CInt(theExtensionIconIndex(extension))
            End If

        Else

            ' No extension known. Use the generic file icons.
            Return FileGenericIconIndex

        End If

    End Function

End Class